package com.yj.lottery_system.service.activityStatus.operator;

import com.yj.lottery_system.dao.dataObject.ActivityDO;
import com.yj.lottery_system.dao.dataObject.ActivityPrizeDO;
import com.yj.lottery_system.dao.mapper.ActivityMapper;
import com.yj.lottery_system.dao.mapper.ActivityPrizeMapper;
import com.yj.lottery_system.service.dto.ConvertActivityStatusDTO;
import com.yj.lottery_system.service.enums.ActivityPrizeStatusEnum;
import com.yj.lottery_system.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component

public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //活动id
        Long activityId = convertActivityStatusDTO.getActivityId();
        //活动目标扭转状态
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if (null == activityId || null == targetActivityStatus) {
            return false;
        }
        //调用dao
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(null == activityDO) {
            return false;
        }
        //判断当前状态与目标状态
        if(targetActivityStatus.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }
        //该活动下 奖品INIT状态 的个数
       int count =  activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //调用dao,更新状态
        try {
            activityMapper.updateStatus( convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
